package com.zrentech.order.util;

public class StringUtilities {

    public static boolean isBlank(String parameter) {
        return parameter == null || parameter.trim().length() == 0;
    }

    /**
     * Remove all ISO Control characters from the given string.
     * 
     * @param s
     * @return clean String
     */
    public static String stripControlCharacters(String s) {
        if (null == s) {
            return null;
        }

        StringBuilder buffer = new StringBuilder(s.length());
        for (char c : s.toCharArray()) {
            if (!Character.isISOControl(c)) {
                buffer.append(c);
            }
        }

        return buffer.toString();
    }

    /**
     * Remove contiguous spaces from the given string.
     * 
     * @param s
     * @return clean String
     */
    public static String stripContiguousSpaces(String s) {
        if (null == s) {
            return null;
        }

        StringBuilder buffer = new StringBuilder(s.length());

        boolean inSpaceMode = false;
        for (char c : s.toCharArray()) {
            if (c == ' ') {
                if (!inSpaceMode) {
                    buffer.append(c);
                    inSpaceMode = true;
                }
            }
            else {
                buffer.append(c);
                inSpaceMode = false;
            }
        }

        return buffer.toString();
    }

    /**
     * Adds double quotes around a string if it does not already begin with a double quotes
     * 
     * @param s -
     *            the string to be quoted
     * @return s surrounded by ", or null if s is null.
     */
    public static String makeQuotedString(String s) {
        if (null == s)
            return null;

        if (s.indexOf("\"") != 0) {
            return "\"" + s + "\"";
        }
        else {
            return s;
        }
    }

    public static String stripHyphens(String string) {
        return string.replaceAll("\\-", "");
    }

    /**
     * Given a string and an array of "bad strings", walks through the array of bad strings and removes these strings
     * (and any trailing spaces) if the occur at the end of the given string. Continues to walk through the list of bad
     * endings and remove endings until no changes are made.
     * 
     * @param s -
     *            the string to strip endings from
     * @param badEndings -
     *            the list of bad strings to remove from the end of s
     * @return a string with all occurrences of the bad endings and all trailing spaces removed, which may be empty.
     */
    public static String stripTrailingStrings(String s, String[] badEndings) {
        if (s == null || s.length() == 0)
            return s;
        if (badEndings == null || badEndings.length == 0)
            return s;

        boolean changes;
        s = s.trim();
        do {
            changes = false;
            for (String badEnding : badEndings) {
                if (s.endsWith(badEnding)) {
                    s = s.substring(0, s.length() - badEnding.length()).trim();
                    changes = true;
                }
            }
        }
        while (changes && s.length() > 0);
        return s;
    }

    public static boolean isQuotedString(String s) {
        return (s.length() > 1 && s.startsWith("\"") && s.endsWith("\""));
    }

}